import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';

import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-advance-collection',
  templateUrl: './advance-collection.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class AdvanceCollectionComponent extends BasicTableComponent {

  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  constructor(
    public service: FreightAccountService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
    }
    return requestOptions;
  };

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_reportYskBla);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          }
        },
        arto: {
          type: 'string',
          title: '付款人',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => this.service.getEnterpriceList({ enterpriseName: q })
          }
        },
        brmtype: {
          type: 'string',
          title: '收款类型',
          enum: [{ label: '全部', value: null }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: null
        },
        banktype: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: null },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: null
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '网络货运人', width: 160, index: 'ltdName' },
      { title: '银行类型', width: 120, index: 'banktypeLabel' },
      { title: '收款账户', width: 150, index: 'ltdaccountId' },
      { title: '收款类型', width: 120, index: 'brmtypeLabel' },
      {
        title: '预收余额',
        index: 'yskmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yskmoney }) }
      },
      { title: '付款人', index: 'artoName', width: 180 },
      { title: '结算客户', index: 'cnoName', width: 150 },
      {
        title: '操作',
        width: 120,
        className: 'text-center',
        fixed: 'right',
        buttons: [
          {
            text: '浏览',
            click: item => this.router.navigate([`/financial-management/advance-collection/detail/${item.id}`])
          }
          // {
          //   text: '核销'
          // },
          // {
          //   text: '退款'
          // }
        ]
      }
    ];
  }
}
